/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PaneMain.java
 *
 * Created on 2011-9-20, 16:21:57
 */
package word_help.view;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JTextField;
import word_help.InitWord;
import word_help.Period;
import word_help.Unit;
import word_help.Word;

/**
 *
 * @author luotao
 */
public class PaneMain extends javax.swing.JPanel {

    private InitWord initWord = new InitWord();

    /** Creates new form PaneMain */
    public PaneMain() {
        try {
            initComponents();
            initWord.init();
            //  but_start.requestFocus();
            requestFocus();
        } catch (Exception ex) {
            Logger.getLogger(PaneMain.class.getName()).log(Level.SEVERE, null, ex);
            throw new RuntimeException(ex.getMessage());
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cob_perd = new javax.swing.JComboBox();
        lab_perdid = new javax.swing.JLabel();
        cob_unit = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        tf_ch = new JTextField();
        jLabel3 = new javax.swing.JLabel();
        tf_en = new JTextField();
        but_start = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        lab_count = new javax.swing.JLabel();
        lab_tip = new javax.swing.JLabel();
        jCheckBox1 = new javax.swing.JCheckBox();
        jButton1 = new javax.swing.JButton();
        tf_count = new JTextField();
        jLabel4 = new javax.swing.JLabel();
        tip_input_result = new javax.swing.JLabel();

        cob_perd.setFont(new java.awt.Font("Lucida Grande", 0, 18));
        cob_perd.setModel(new PerdComboModel());
        cob_perd.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cob_perdItemStateChanged(evt);
            }
        });
        cob_perd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cob_perdActionPerformed(evt);
            }
        });

        lab_perdid.setFont(new java.awt.Font("Lucida Grande", 0, 18)); // NOI18N
        lab_perdid.setText("Period：");

        cob_unit.setFont(new java.awt.Font("Lucida Grande", 0, 18)); // NOI18N
        cob_unit.setModel(new DefaultComboBoxModel());
        cob_unit.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cob_unitItemStateChanged(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Lucida Grande", 0, 18)); // NOI18N
        jLabel1.setText("Unit：");

        jLabel2.setFont(new java.awt.Font("Lucida Grande", 0, 18)); // NOI18N
        jLabel2.setText("chinese:");

        tf_ch.setFont(new java.awt.Font("Lucida Grande", 0, 18));

        jLabel3.setFont(new java.awt.Font("Lucida Grande", 0, 18)); // NOI18N
        jLabel3.setText("english:");

        tf_en.setFont(new java.awt.Font("Lucida Grande", 0, 18));
        tf_en.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tf_enActionPerformed(evt);
            }
        });

        but_start.setFont(new java.awt.Font("Lucida Grande", 0, 18)); // NOI18N
        but_start.setText("start");
        but_start.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                but_startActionPerformed(evt);
            }
        });
        but_start.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                but_startKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                but_startKeyReleased(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Lucida Grande", 0, 18)); // NOI18N
        jLabel5.setText("剩余：");

        lab_count.setFont(new java.awt.Font("Lucida Grande", 0, 18));
        lab_count.setText("    ");

        lab_tip.setFont(new java.awt.Font("Lucida Grande", 0, 18));
        lab_tip.setForeground(new java.awt.Color(255, 0, 0));
        lab_tip.setText(" ");

        jCheckBox1.setFont(new java.awt.Font("Lucida Grande", 0, 18)); // NOI18N
        jCheckBox1.setText("test");
        jCheckBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox1ActionPerformed(evt);
            }
        });

        jButton1.setFont(new java.awt.Font("Lucida Grande", 0, 18)); // NOI18N
        jButton1.setText("next");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        tf_count.setFont(new java.awt.Font("Lucida Grande", 0, 18));
        tf_count.setText("1");

        jLabel4.setFont(new java.awt.Font("Lucida Grande", 0, 18)); // NOI18N
        jLabel4.setText("times:");

        tip_input_result.setFont(new java.awt.Font("Lucida Grande", 0, 18)); // NOI18N
        tip_input_result.setForeground(new java.awt.Color(255, 0, 0));
        tip_input_result.setText(" ");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(49, 49, 49)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jLabel3)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(lab_perdid, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addComponent(jLabel4))
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(tf_count, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 515, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(tf_en, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 444, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(cob_perd, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel1)
                                .addGap(18, 18, 18)
                                .addComponent(cob_unit, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 68, Short.MAX_VALUE)
                                .addComponent(but_start))
                            .addComponent(tf_ch, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 444, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(lab_tip, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 103, Short.MAX_VALUE)
                                .addComponent(tip_input_result, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(26, 26, 26)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jButton1)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(lab_count, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jCheckBox1)
                                .addGap(36, 36, 36)))))
                .addGap(47, 47, 47))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(39, 39, 39)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lab_perdid)
                    .addComponent(cob_perd, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(but_start)
                    .addComponent(jLabel1)
                    .addComponent(cob_unit, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jCheckBox1))
                .addGap(17, 17, 17)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tf_count, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(tf_ch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(lab_count))
                .addGap(35, 35, 35)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(tf_en, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lab_tip)
                    .addComponent(tip_input_result))
                .addGap(84, 84, 84))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {but_start, cob_perd, cob_unit, jCheckBox1, jLabel1, lab_perdid});

    }// </editor-fold>//GEN-END:initComponents

    private void cob_perdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cob_perdActionPerformed
    }//GEN-LAST:event_cob_perdActionPerformed

    private void cob_perdItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cob_perdItemStateChanged
        // TODO add your handling code here:
        //  Period comboModel = (Period) evt.getSource();
        Period comboModel = (Period) evt.getItem();
        cob_unit.setModel(new UnitComboModel(comboModel.getMap()));
        // TODO add your handling code here:
    }//GEN-LAST:event_cob_perdItemStateChanged

    private void but_startActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_but_startActionPerformed
        // TODO add your handling code here:
        initWord();
        selectWord();
    }//GEN-LAST:event_but_startActionPerformed

    private void selectWord() {
        if (selectWords.size() < 1) {
            return;
        }
        int random = new Random().nextInt(selectWords.size());
        Word word = (Word) selectWords.get(random);
        currentWord = word;

        tf_en.setText("");
        tf_ch.setText(word.getChName());
        lab_count.setText(selectWords.size() + "");
        List<String> types = word.getTypes();
        for (int i = 0; i < types.size(); i++) {
            StringBuilder sb = new StringBuilder();
            sb.append(types.get(i));
            if (i != 0) {
                sb.append(" , ");
            }
        }

        showWord();

    }

    private void showWord() {
        if (jCheckBox1.isSelected()) {
            //      tf_en.setText(word.getEnName());
            lab_tip.setText(currentWord.getEnName());
        } else {
            //      tf_en.setText("");
            lab_tip.setText("");
        }
    }

    private void initWord() {
        selectWords.clear();
        Unit unit = (Unit) cob_unit.getSelectedItem();
        Map<String, Word> map = unit.words;
        for (Word word : map.values()) {
            selectWords.add(word);
        }
    }

    private void cob_unitItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cob_unitItemStateChanged
        // TODO add your handling code here:
        initWord();

    }//GEN-LAST:event_cob_unitItemStateChanged

    private void tf_enActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tf_enActionPerformed
        // TODO add your handling code here:

        JTextField tf = (JTextField) evt.getSource();
        String input = tf.getText();
        int input_count = Integer.valueOf(tf_count.getText());
        tip_input_result.setText("");
        if (input.trim().equalsIgnoreCase(currentWord.getEnName().trim())) {
            if (current_index >= input_count || !jCheckBox1.isSelected()) {
                selectWords.remove(currentWord);
                selectWord();
                current_index = 1;
                return;
            } else {
                current_index++;
                tf_en.setText("");
            }
        } else {
            tip_input_result.setText("ERROR");
        }

    }//GEN-LAST:event_tf_enActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        selectWords.remove(currentWord);
        selectWord();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void but_startKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_but_startKeyReleased
        // TODO add your handling code here:
    }//GEN-LAST:event_but_startKeyReleased

    private void but_startKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_but_startKeyPressed
        // TODO add your handling code here:
        System.out.println("evt = " + evt.getKeyCode());
    }//GEN-LAST:event_but_startKeyPressed

    private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox1ActionPerformed
        // TODO add your handling code here
        showWord();
    }//GEN-LAST:event_jCheckBox1ActionPerformed

    private class UnitComboModel extends DefaultComboBoxModel {

        private Map data;

        public UnitComboModel(Map data) {
            this.data = data;
        }

        @Override
        public Object getElementAt(int index) {
            return data.values().toArray()[index];
        }

        @Override
        public int getSize() {
            return data.keySet().toArray().length;
        }
    }

    private class PerdComboModel extends DefaultComboBoxModel {

        @Override
        public Object getElementAt(int index) {
            return initWord.getPerds().get(index);
        }

        @Override
        public int getSize() {
            return initWord.getPerds().size();
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton but_start;
    private javax.swing.JComboBox cob_perd;
    private javax.swing.JComboBox cob_unit;
    private javax.swing.JButton jButton1;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel lab_count;
    private javax.swing.JLabel lab_perdid;
    private javax.swing.JLabel lab_tip;
    private JTextField tf_ch;
    private JTextField tf_count;
    private JTextField tf_en;
    private javax.swing.JLabel tip_input_result;
    // End of variables declaration//GEN-END:variables
    private List selectWords = new ArrayList();
    private Word currentWord;
    private int current_index = 1;
}
